﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditPoster : System.Web.UI.UserControl
    {
        public string imagesposter;
        public string nameposter;
        public string link;
        public string placeposter;
        public string titleposter;
        public string sizeposter;
        public int posterid;
        public string filePathSlide;
        protected void Page_Load(object sender, EventArgs e)
        {
            
                posterid = Convert.ToInt32(Request.QueryString["idposter"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Poster", "Poster_ID", posterid.ToString());
                imagesposter = dt.Rows[0]["Poster_Image"].ToString();
                link = dt.Rows[0]["Poster_Link"].ToString();
                nameposter = dt.Rows[0]["Poster_TieuDe"].ToString();
                placeposter = dt.Rows[0]["Poster_Place"].ToString(); ;
                titleposter = dt.Rows[0]["Poster_Title"].ToString(); ;
                sizeposter = dt.Rows[0]["Poster_Size"].ToString(); ;
                txtName.Text = nameposter;
                txtPlace.Text = placeposter;
                txtLink.Text = link;
                txtTitle.Text = titleposter;
                txtSize.Text = sizeposter;
        }

        protected void btnUpdatePoster_Click(object sender, EventArgs e)
        {
            if (txtLink.Text != "")
            {
                if (fulImagePoster.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImagePoster.FileName))
                    {
                        if (imagesposter != "")
                        {
                            string linkimage = fulImagePoster.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Poster/"), linkimage, 940, fulImagePoster.PostedFile.InputStream);
                            filePathSlide = "/Resources/Images/Images-Poster/" + fulImagePoster.FileName;
                            //File.Delete(MapPath(imagesposter));
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                            cl[0] = new DAL.DHelpers.Column("Poster_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Poster_Place", txtPlace.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Poster_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Poster_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Poster_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[5] = new DAL.DHelpers.Column("Poster_Size", txtSize.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string[] colWhere = new string[1];
                            colWhere[0] = "Poster_ID";

                            object[] colValue = new object[1];
                            colValue[0] = posterid;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Poster", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=5&sub=4");
                        }
                        else
                        {
                            string linkimage = fulImagePoster.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images-Poster/"), linkimage, 940, fulImagePoster.PostedFile.InputStream);
                            filePathSlide = "/Resources/Images-Poster/" + fulImagePoster.FileName;
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                            cl[0] = new DAL.DHelpers.Column("Poster_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Poster_Place", txtPlace.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Poster_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Poster_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Poster_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[5] = new DAL.DHelpers.Column("Poster_Size", txtSize.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string[] colWhere = new string[1];
                            colWhere[0] = "Poster_ID";

                            object[] colValue = new object[1];
                            colValue[0] = posterid;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Poster", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=5&sub=4");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }
                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                    cl[0] = new DAL.DHelpers.Column("Poster_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[1] = new DAL.DHelpers.Column("Poster_Place", txtPlace.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[2] = new DAL.DHelpers.Column("Poster_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[3] = new DAL.DHelpers.Column("Poster_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[4] = new DAL.DHelpers.Column("Poster_Size", txtSize.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    string[] colWhere = new string[1];
                    colWhere[0] = "Poster_ID";

                    object[] colValue = new object[1];
                    colValue[0] = posterid;

                    DAL.DCollector.DBCollector.Instance.Update("tbl_Poster", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=5&sub=4");
                }

            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập link liên kết...')</script>");
            }
        }
    }
}